package com.songshuai.sales.client.aop;

import com.alibaba.fastjson.JSON;
import com.songshuai.webapi.baseapi.pojo.SysLog;
import com.songshuai.webapi.common.log.BaseSysLoggerAspect;
import com.songshuai.webapi.common.mqchannel.OutputSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * Created by yubo on 2018/12/6.
 */
@Aspect
@Component
@EnableBinding(OutputSource.class)
public class SysLoggerAspect extends BaseSysLoggerAspect {

    @Autowired
    @Output(OutputSource.CUSTPERMISSONCHANNEL)
    private MessageChannel custpermissonchannel;

    private SysLog sysLog;

    @Pointcut("@annotation(com.songshuai.webapi.baseapi.annotations.SysLogger)")
    public void loggerPointCut() {

    }

    @Before("loggerPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        this.sysLog = new SysLog();
        sentSysLog(joinPoint,sysLog);
    }

    @Around("loggerPointCut()")
    public Object arround(ProceedingJoinPoint pjp) throws Throwable {
        Object result = pjp.proceed();
        sysLog.setResult(JSON.toJSONString(result));
        return result;
    }
    @After("loggerPointCut()")
    public void after() {
        custpermissonchannel.send(MessageBuilder.withPayload(JSON.toJSONString(sysLog)).build());
    }


}

